<?php

namespace common\logic\sms;

use common\logic\logs\FLogHandler;
use common\logic\logs\Log;
use common\tool\HttpCurl;

class SingleSend implements SendMsgInterface{
    const NAME = "sign";
    
    const APIKEY = 'apikey';
    const MOBILE = 'mobile';
    const TEXT = 'text';
    
    protected $must;//参数
    public function __construct(){
        $this->must = [self::APIKEY,self::MOBILE,self::TEXT];
    }
    
    /**
     * 单条短信发送,智能匹配短信模板
     *
     * @param $message 参数数组
     * apikey成功注册后登录云片官网,进入后台可查看
     * @param text需要使用已审核通过的模板或者默认模板
     * @param mobile接收的手机号,仅支持单号码发送
     * @return json格式字符串
     */
    public function send(array $params){
        $r = SmsCommon::verifyParam($params, $this->must);
        if($r){
            return $r;
        }

//        $res = HttpCurl::curlRequest(Configs::smsurl.Configs::single_send_https, $params, 'POST');
        $res = '{
	"code": 0,
	"msg": "发送成功",
	"count": 1,
	"fee": 0.05,
	"unit": "RMB",
	"mobile": "18736199128",
	"sid": 61956203368
}';
        $res = json_decode($res, true);
        if($res === false || 0 != $res['code']) {
            $this->sendFail($params, $res);
        }
        $this->sendSuccess($params, $res);
    }
    
    //发送成功
    public function sendSuccess(array $sendData, array $sendRes){
        $this->sendLog($sendData, $sendRes);
    }
    
    //发送失败
    public function sendFail(array $sendData, array $sendRes){
        $this->sendLog($sendData, $sendRes);
    }
    
    //发送日志
    public function sendLog(array $sendData, array $sendRes){
        $logHandler = new FLogHandler(__DIR__."/logs/".date('Y-m-d').'.log');
        Log::Init($logHandler, 15, 1);
        Log::Info(json_encode($sendData, JSON_UNESCAPED_UNICODE));
        Log::Info(json_encode($sendRes, JSON_UNESCAPED_UNICODE));
        echo  'ok';
    }
}